﻿var url = `${bddhWebsocketUrl}/tel`;

/* 拨 号  */
export function tel(callback, tag) {
    tag = tag || "";
    var cmd = "tel>" + tag + ">0";
    send(callback, cmd);
}

/* 状 态 */
export function telstate(callback, tag) {
    tag = tag || "";
    var cmd = "telstate>" + tag;
    send(callback, cmd);
}

//挂 断
export function telend(callback, tag) {
    tag = tag || "";
    var cmd = "telend>" + tag;
    send(callback, cmd);
}



export function send(callback, message) {
    if ("WebSocket" in window) { //window.websocket

        var websocket = new WebSocket(url);
        websocket.onopen = function (e) {
            websocket.send(message);
        };
        websocket.onmessage = function (e) {
            websocket.close();
            if (typeof callback == "function") {
                callback(e.data);
            }
        };
        websocket.onerror = function (e) {
            if (typeof callback == "function") {
                var errormsg = '{MsgType:"ERROR",Data:"",Tag:""}';
                callback(errormsg)
            }
        };
    } else {
        alert("您的浏览器不支持 WebSocket!");
    }
}
